<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2021-03-18 22:48:30
 */

namespace app\admin\logic;
use app\common\logic\BaseLogic as BaseLogic;

class Histmenu extends BaseLogic {

    public function __construct() {
        parent::__construct();
        
		$this->histmenuModel = model('SystemHistmenu', 'admin');
	}
	
	public function getHistmenu($nowRule, $adminid, $nowtime) {
		//echo '当前菜单：';print_r($nowRule);
		//$module = MODULE_NAME; //模块名
		//$controller = CONTROLLER_NAME; //控制器名
		//$module = strtolower($module);
		//$controller = strtolower($controller);
		//echo '当前模块名：'.$module;echo '当前控制器名：'.$controller;
		
		if ($nowRule['id']) {
			$checkInfo = $this->histmenuModel->findMap([['adminid', '=', $adminid], ['module', '=', $nowRule['module']], ['controller', '=', $nowRule['controller']]]);
			$data = [
				'name' 			=> $nowRule['name'],
				'module' 		=> $nowRule['module'],
				'controller' 	=> $nowRule['controller'],
				'action' 		=> $nowRule['action'],
				'parameter' 	=> $nowRule['parameter'],
			];
			if ($checkInfo) {
				$this->histmenuModel->baseUpdateData($checkInfo['id'], $data);
			} else {
				$data['adminid'] = $adminid;
				$data['create_time'] = $nowtime;
				$this->histmenuModel->baseAddData($data);
			}
		}
		
		//获取历史菜单列表
		list($histmenuList, $ids) = $this->histmenuModel->search([['adminid', '=', $adminid]]);
		$ids = array_values($ids);
		//print_r($ids);
		//$flippedArray = array_flip($ids);
		//print_r($flippedArray);
		$retHistmenuList = [];
		foreach ($histmenuList as $key => $value) {
			if ($nowRule['id'] && ($nowRule['module'] == $value['module'] && $nowRule['controller'] == $value['controller'])) {
				$value['current'] = 1;
			} else {
				$value['current'] = 0;
			}
			foreach ($ids as $key2 => $value2) {
				if ($value['id'] == $value2) {
					$nowKey = $key2;
				}
			}
			if (isset($ids[$nowKey+1])) {
				$value['nexturl'] = $histmenuList[$ids[$nowKey+1]]['url'];
			} else {
				$value['nexturl'] = url('admin/index/index');
			}
			$retHistmenuList[] = $value;
		}
		//print_r($retHistmenuList);
		
		if ($nowRule['module'] == 'admin' && $nowRule['controller'] == 'index') {
			$indexCurrent = 1;
		} else {
			$indexCurrent = 0;
		}
		
		return [$retHistmenuList, $indexCurrent];
	}
	
	public function closeHistmenu($id, $adminid, $nowtime) {
		$this->histmenuModel->delInfo($id);
		
	}
	
}